https://leetcode.cn/problems/cut-off-trees-for-golf-event/
class Solution {
    typedef pair<int,int> PII;
    int dx[4]={0,0,1,-1};
    int dy[4]={1,-1,0,0};
    int m,n;
    int visi[51][51];
public:
    int cutOffTree(vector<vector<int>>& forest) {
        m=forest.size(),n=forest[0].size();
        
        //顺序化 1- 2- 3- 
        vector<PII> trees;
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(forest[i][j]>1)
                {
                    trees.push_back({i,j});
                }
            }
        }
        cout<<endl;
        sort(trees.begin(),trees.end(),[&](const PII & p1,const PII& p2){
            return forest[p1.first][p1.second]<forest[p2.first][p2.second];
        });

        //转化为迷宫问题
        int ret=0;
        int x=0,y=0;
        for(auto &[a,b]:trees)
        {
            cout<<" ["<<x<<" "<<y<<"]:"<<"["<<a<<" "<<b<<"]  ";
            int step=bfs(forest,x,y,a,b);
            //找不到出口
            if(step==-1)
                return -1;
            ret+=step;
            x=a,y=b;
        }
        return ret;
    }

    int bfs(vector<vector<int>>& forest,int bx,int by,int ea,int eb)
    {
        if(bx==ea&&by==eb)
            return 0;
        queue<PII> q;
        q.push({bx,by});
        memset(&visi,0,sizeof(visi));
        visi[bx][by]=true;
        
        int step=0;
        while(!q.empty())
        {
            //需要扩展的数目
            int sz=q.size();
            step++;
            while(sz--)
            {
                auto [a,b]=q.front();
                q.pop();
                for(int i=0;i<4;i++)
                {
                    int x=a+dx[i],y=b+dy[i];
                    if(x>=0&&x<m&&y>=0&&y<n&&!visi[x][y]&&forest[x][y])
                    {
                        if(x==ea&&y==eb) return step;

                        visi[x][y]=true;
                        q.push({x,y});
                    }
                    
                }
            }
            cout<<step<<endl;
        }
        return -1;
    }
};